package ca.etsmtl.log730.projet.controller.server;

import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.example.sumup.message.AbstractMessage;

import ca.etsmtl.log730.projet.controller.AbstractSessionController;
import ca.etsmtl.log730.projet.controller.nmessages.Reply;
import ca.etsmtl.log730.projet.controller.nmessages.ReplyType;
import ca.etsmtl.log730.projet.controller.nmessages.commands.FileCommand;
import ca.etsmtl.log730.projet.controller.nmessages.commands.SessionAction;
import ca.etsmtl.log730.projet.controller.nmessages.commands.SessionCommand;
import ca.etsmtl.log730.projet.model.user.User;

public class ServerClientChannelHandler extends AbstractSessionController {

	private WorkspaceManager workspace = new WorkspaceManager();
	private Map<Long, Serializable> messagesMap = new TreeMap<Long, Serializable>();
	
	public Map<Long, Serializable> getMessagesMap() {
		return messagesMap;
	}


	public void setMessagesMap(Map<Long, Serializable> messagesMap) {
		this.messagesMap = messagesMap;
	}


	public void addEvent(AbstractMessage am) {
		messagesMap.put(System.currentTimeMillis(), am);
	}
	
	
	public WorkspaceManager getWorkspace() {
		return workspace;
	}

	public ServerClientChannelHandler(IoSession session, UUID sessionId) {
		super(session, sessionId);
	}

	@Override
	public void exceptionCaught(IoSession ios, Throwable arg1)
			throws Exception {
	}

	@Override
	public void messageReceived(final IoSession ios, Object o)
			throws Exception {
		/*NAME SWITCH*/
		MainServer.getLogger().info(o.toString());
		if(o instanceof SessionCommand) {
			SessionCommand sc = ((SessionCommand)o);
			if(sc.getAction() == SessionAction.IDENT) {
				this.workspace.addUser(UUID.randomUUID(), (User)sc.getParam());
//				super.writeObject(ios, new Reply(sc.ID, ReplyType.OK));
			}
		} else if(o instanceof FileCommand) {
			FileCommand fc = (FileCommand) o;
			switch(fc.getAction().ordinal()) {
			
			case 0:
				this.workspace.addObject(fc.getObject());
				break;
			case 4:
				this.workspace.removeObject(fc.getFileId());
			default:
				break;
			}
		}
	}

	@Override
	public void messageSent(IoSession ios, Object o) throws Exception {
		addEvent((AbstractMessage)o);
	}

	@Override
	public void sessionClosed(IoSession ios) throws Exception {
		User tt = null;
		for(Entry<UUID, User> e: this.workspace.getUsers().entrySet()) {
			if(e.getValue().getSession() == ios) {
				tt = e.getValue();
			}
		}
		if(tt != null) workspace.getUsers().remove(tt);

	}

	@Override
	public void sessionCreated(IoSession ios) throws Exception {
		MainServer.getLogger().info("NEW CONNECTION!");
//		writeObject(ios,"HOLA");
//		Welcome nsi = new Welcome();
//		clientList.add(new ServerClientChannelHandler(ios,nsi.getSessionId()));
//		User uu = new User("RANDOMSHITE");
//		uu.setSession(ios);
//		workspace.addUser(nsi.getSessionId(), uu);
//		writeObject(ios, nsi);
	}

	@Override
	public void sessionIdle(IoSession ios, IdleStatus arg1)
			throws Exception {
	}

	@Override
	public void sessionOpened(IoSession ios) throws Exception {
	}

	
//	public List<ServerClientChannelHandler> getClientList() {
//		return clientList;
//	}

	
}
